<?php

/**
 * @Entity(repositoryClass="DefaultDb_Repositories_UserRepository")
 * @Table(name="users")
 */
class DefaultDb_Entities_User
{
    /** constantes para tipo de usuario.
     * IMPORTANTE!!!
     * Si agregar una nueva constante, no olvides definir un case en la function
     * getTypeString. 
     */
    const USER_ADMIN = 1;
    const USER_DRIVER = 2;
    const USER_CLIENT = 3;
    const USER_SECRETARY = 4;
    const USER_CLIENT_MAS_DISTRIBUCION = 5;
    const USER_STORER = 6; // almacenista
    const USER_OPERATION_CONTROLLER = 7; // controlador de operaciones
    const USER_CEO = 8;

    /* constantes para tipo de usuario */
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
    const STATUS_BLOCK = 2;

    /* constantes para el dia que desea tener el usuario para facturar */
    const DAY_MONDAY = 1;
    const DAY_TUESDAY = 2;
    const DAY_WEDNESDAY = 3;
    const DAY_THURSDAY = 4;
    const DAY_FRIDAY = 5;
    const DAY_SATURDAR = 6;
    const DAY_SUNDAY = 7;

    /**
     * @Id @GeneratedValue @Column(type="integer")
     * @var integer
     */
    protected $id;

    /**
     * @Column(type="string", name="code")
     * @var string
     */
    protected $code;

    /**
     * @Column(type="integer", name="status" , options={"default" = 1})
     * @var integer
     */
    protected $status = 1;

    /**
     * @Column(type="string", name="first_name")
     * @var string
     */
    protected $firstName;

    /**
     * @Column(type="string", name="last_name")
     * @var string
     */
    protected $lastName;

    /**
     * @Column(type="string", name="username")
     * @var string
     */
    protected $username;

    /**
     * @Column(type="string", name="password")
     * @var string
     */
    protected $password;

    /**
     * @Column(type="string", name="title", nullable=true)
     * @var string
     */
    protected $title;

    /**
     * @Column(type="string", name="commercial_name", nullable=true)
     * @var string
     */
    protected $commercialName;

    /**
     * @Column(type="string", name="cell_phone", nullable=true)
     * @var string
     */
    protected $cellPhone;

    /**
     * @Column(type="string", name="local_number", nullable=true)
     * @var string
     */
    protected $localNumber;

    /**
     * @Column(type="string", name="type", nullable=true)
     * @var string
     */
    protected $type;

    /**
     * @OneToMany(targetEntity="DefaultDb_Entities_Catalog", mappedBy="client")
     * */
    private $catalogs;

    /**
     * @OneToMany(targetEntity="DefaultDb_Entities_M3CommerceOrder", mappedBy="seller")
     * */
    private $ordersReceived;

    /**
     * @OneToMany(targetEntity="DefaultDb_Entities_M3CommerceOrder", mappedBy="buyer")
     * */
    private $ordersGenerated;

    /**
     * @ManyToOne(targetEntity="DefaultDb_Entities_Category")
     */
    protected $category;

    /**
     * @ManyToOne(targetEntity="DefaultDb_Entities_Point")
     */
    protected $point = 0;

    /**
     * @Column(type="integer", name="dayInvoice")
     * @var integer
     */
    protected $dayInvoice = 0;

    /**
     * @OneToMany(targetEntity="DefaultDb_Entities_BranchesUser", mappedBy="client")
     * */
    private $branches;

    /**
     * @ManyToOne(targetEntity="DefaultDb_Entities_User")
     */
    protected $parent = null;

    public function getId()
    {
        return $this->id;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function getFirstName()
    {
        return $this->firstName;
    }

    public function getLastName()
    {
        return $this->lastName;
    }

    public function getFullName()
    {
        return $this->firstName . ' ' . $this->lastName;
    }

    public function getFullNameWithType()
    {
        return $this->firstName . ' ' . $this->lastName . ' [' . $this->getTypeString() . ']';
    }

    public function getFullNameWithCode()
    {
        $str = $this->firstName . ' ' . $this->lastName;
        if ($this->code != '')
        {
            $str .= (' [' . $this->code . ']');
        }
        return $str;
    }

    public function getCode()
    {
        return $this->code;
    }

    public function setCode($code)
    {
        $this->code = $code;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function getCommercialName()
    {
        return $this->commercialName;
    }

    public function getType()
    {
        return $this->type;
    }

    public function getCellPhone()
    {
        return $this->cellPhone;
    }

    public function getLocalNumber()
    {
        return $this->localNumber;
    }

    public function getCategory()
    {
        return $this->category;
    }

    public function getDayInvoice()
    {
        return $this->dayInvoice;
    }

    public function getBranches()
    {
        return $this->branches;
    }

    public function getParent()
    {
        return $this->parent;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function setStatus($status)
    {
        $this->status = $status;
    }

    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;
    }

    public function setLastName($lastName)
    {
        $this->lastName = $lastName;
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function setCommercialName($commercialName)
    {
        $this->commercialName = $commercialName;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function setCellPhone($cellPhone)
    {
        $this->cellPhone = $cellPhone;
    }

    public function setLocalNumber($localNumber)
    {
        $this->localNumber = $localNumber;
    }

    public function setCategory($category)
    {
        $this->category = $category;
    }

    public function setPoint($point)
    {
        $this->point = $point;
    }

    public function setDayInvoice($dayInvoice)
    {
        $this->dayInvoice = $dayInvoice;
    }

    public function setParent($parent)
    {
        $this->parent = $parent;
    }

    public function getCatalogs()
    {
        return $this->catalogs;
    }

    public function getOrdersReceived()
    {
        return $this->ordersReceived;
    }

    public function getOrdersGenerated()
    {
        return $this->ordersGenerated;
    }

    public function getData()
    {
        $data = array();
        $data['id'] = $this->id;
        $data['firstName'] = $this->firstName;
        $data['lastName'] = $this->lastName;
        $data['username'] = $this->username;
        $data['type'] = $this->type;
        return $data;
    }

    public function getPoint()
    {
        return $this->point;
    }

    public function getNameExchangeCenter()
    {
        $name = null;
        if ($this->type == self::USER_STORER)
        {
            if ($this->point !== null)
            {
                $ci = $this->getPoint();
                $name = $ci->getName();
            }
        }
        return $name;
    }

    public function getTypeString()
    {
        return self::getStaticTypeString($this->type);
    }

    public static function getStaticTypeString($type)
    {
        $str = '';
        switch ($type)
        {
            case self::USER_ADMIN: $str = 'Administrador';
                break;
            case self::USER_CLIENT: $str = 'Cliente';
                break;
            case self::USER_DRIVER: $str = 'Conductor';
                break;
            case self::USER_SECRETARY: $str = 'Secretaria';
                break;
            case self::USER_STORER: $str = 'Almacenista';
                break;
            case self::USER_OPERATION_CONTROLLER: $str = 'Controlador de operaciones';
                break;
            case self::USER_CEO: $str = 'Director ejecutivo';
                break;
        }
        return $str;
    }

    public static function generateOptionsForType()
    {
        $oClass = new ReflectionClass(__CLASS__);
        $consts = $oClass->getConstants();
        $keys = array_keys($consts);
        $constsType = array_filter($keys, function($const) {
                    return preg_match('/^USER_/i', $const) != 0;
                });
        $options = array();
        array_walk($constsType, function($const) use(&$options) {
                    $nameConst = 'self::' . $const;
                    $valueConst = constant($nameConst);
                    $options[] = '<option value="' . $valueConst . '">' . self::getStaticTypeString($valueConst) . '</option>';
                });
        return implode(PHP_EOL, $options);
    }

}
